#include "ifeedreader_engine.h"
#include "xmlrpc_support.h"


#include <set>

using namespace std;

class TestEngine : public IFeedReaderEngine
{
public:
        virtual set<Tag*>       GetTags ();
        virtual set<Feed*>      GetFeeds ();
        virtual Feed            GetFeedByNumber ( int feedID );
        virtual News            GetNewsByNumber ( int newsID );
        virtual vector<News*>   GetNews  ( int page , int count ) ;

	virtual int	        AddFeed ( string name , string url );
	virtual int             DelFeed ( int feedID );

	virtual int             AssignTags ( int newsID , set<int> tags );
        virtual int             RemoveTags ( int newsID , set<int> tags );
};


set<Tag*> TestEngine::GetTags ()
{
	set<Tag*> st;
	Tag* t1 = new Tag ();
	t1->TagID = 1;
	t1->Name = "Deportes";
	st.insert(t1);
	t1 = new Tag ();
	t1->TagID = 2;
	t1->Name = "Tenos";
	st.insert(t1);
	return st;
}

set<Feed*> TestEngine::GetFeeds ()
{
	set<Feed*> retval;
	
	Feed* f1 = new Feed ();
	f1->FeedID = 1;
	f1->Name = "test feed 1";
	f1->Url = "http://abajodatos.com";
	f1->DateTime = 12345;
	retval.insert ( f1 );

	f1 = new Feed();
	f1->FeedID = 2;
	f1->Name = "Pingocho Mamon";
	f1->Url = "http://fuckdatos.com";
	f1->DateTime = 54321;
	retval.insert(f1);

	return retval;
};

Feed TestEngine::GetFeedByNumber ( int feedID )
{
	Feed feed;
	feed.FeedID = feedID;
	feed.Name = "GetFeedByNumber Test";
	feed.Url = "http://datosgohome.com.ar";
	feed.DateTime = 1234;
	return feed;
}

News TestEngine::GetNewsByNumber ( int newsID )
{
	News ns;
	ns.NewsID = newsID;
	ns.Title = "Datos is crap";
	ns.Description = "Conductor borracho casi provoca una tragedia. Batman unico testigo.";
	ns.Author = "Cronica TV";
	ns.Url = "http://www.cronica.com.ar";
	ns.DateTime = 4321;
	ns.Tags.insert ( 1 );
	ns.Tags.insert ( 2 );
	ns.Tags.insert ( 25 );
	
	return ns;
}

vector<News*> TestEngine::GetNews  ( int page , int count )
{
	vector<News*> retval;

	for ( int i = 10 * page ; i < (10 * page + count) ; i++ )
	{
		News* news = new News ();
		(*news) = GetNewsByNumber(i);
		retval.push_back ( news );
	}
		
	return retval;
}

int TestEngine::AddFeed ( string name , string url )
{
	printf ( "AddFeed ( %s , %s )\n" , name.c_str() , url.c_str() );
	return 0;
}

int TestEngine::DelFeed ( int feedID )
{
	printf ( "DelFeed ( %d )\n" , feedID );
	return 0;
}

int TestEngine::AssignTags ( int newsID , set<int> tags )
{
	
	printf ( "AssignTags ( %d , [" , newsID );
	set<int>::iterator iter;
	for ( iter = tags.begin() ; iter != tags.end() ; iter++ )
		printf ( "%d," , *iter );

	printf ( "] )\n" );
	
	return 0;
}

int TestEngine::RemoveTags ( int newsID , set<int> tags )
{
	printf ( "RemoveTags ( %d , [" , newsID );
	set<int>::iterator iter;
	for ( iter = tags.begin() ; iter != tags.end() ; iter++ )
		printf ( "%d," , *iter );

	printf ( "] )\n" );
	return 1;
}


#include "Thread.h"

class MainThread : public Thread
{
public:
	MainThread() : Thread(false) {};

protected:
	virtual void main ();	
};

#include "iostream.h"

void MainThread::main()
{
	cout << "Soy un thread, rompo todo!!!" << endl;
}


int main(int argc, char** argv)
{
	MainThread mainThread;
	mainThread.start();
	
	TestEngine* te = new TestEngine ();
	FeedReaderXmlRpcSupport::Run ( te , 32678 );
	return 0;
}
